Table of Contents
Discover the API Management service
- Azure API Management
- Azure API Management เป็น APIs Gateway ที่สามารถเอา API ต่างๆมาผูกได้ และสามารถจัดการ caching, security, protection, คลุม Policy หรือเอาไปหา Insight เพิ่มได้เป็นต้น
ซึ่งตัว ประกอบไปด้วย 3 ส่วน- API gateway
- Azure portal จัดการ API Gateway
- Developer portal - API Doc / interactive console / ขอ API Key
- Products
- Open
- Protected - subscribed ก่อนใช้ และ Approve โดย Admin
- Groups
- Administrators
- Developers
- Guests - Unauthenticated / สามารถกำหนดได้แค่ view APIs but not call them
- Policies - กำหนดจาก Azure portal to change the behavior of the API โดยกำหนดกฏด้วย XML
- Explore API gateways

- ถ้าไม่มี API Gateway
- เกิด Coupling ระหว่าง Client กับ Service ถ้าไม่มี API Gateway ฝั่ง Client ซับซ้อนขึ้น เพราะต้องรู้จักกับหลาย End Points เกิด Network Round Trip
- Public Endpoint หลายตัว ยากต่อการจัดการควบคุม
- API Gateway ช่วย - decoupling clients from services โดยมี Design Pattern ดังนี้
- Gateway routing - gateway as a reverse proxy เพื่อ route requests ให้กับ backend services / decouple clients
- Gateway aggregation - client ส่ง request เดียว และ API Gateway dispatches requests ให้กับ backend services / reduce chattiness
- Gateway Offloading - ใส่ Functions เสริมลงไป โดยไม่ต้องไปไล่ทำให้กับทุก backend services (cross-cutting concerns) เช่น
- SSL termination
- Authentication
- IP allow/block list
- Client rate limiting (throttling)
- Logging and monitoring
- Response caching
- GZIP compression
- Servicing static content
- Explore API Management policies
- Understanding policy configuration - XML กำหนดหลัก
inbound,backend,outbound, andon-error - Policy เรียงตามลำดับจากสูงกว่าไปต่ำกว่า
cross-domainก่อนfind-and-replace
<policies>
<inbound>
<!-- statements to be applied to the request go here -->
<!-- cross-domain statement would execute before any higher policies which would in turn, be followed by the find-and-replace policy.-->
<cross-domain />
<base />
<find-and-replace from="xyz" to="abc" />
</inbound>
<backend>
<!-- statements to be applied before the request is forwarded to the backend service go here -->
</backend>
<outbound>
<!-- statements to be applied to the response go here -->
<!-- Filter Response Content Example -->
<base />
<choose>
<when condition="@(context.Response.StatusCode == 200 && context.Product.Name.Equals("Starter"))">
<!-- NOTE that we are not using preserveContent=true when deserializing response body stream into a JSON object since we don't intend to access it again. See details on https://docs.microsoft.com/azure/api-management/api-management-transformation-policies#SetBody -->
<set-body>
@{
var response = context.Response.Body.As<JObject>();
foreach (var key in new [] {"minutely", "hourly", "daily", "flags"}) {
response.Property (key).Remove ();
}
return response.ToString();
}
</set-body>
</when>
</choose>
</outbound>
<on-error>
<!-- statements to be applied if there is an error condition go here -->
</on-error>
</policies>- Create advanced policies
- Control flow - Conditionally applies policy statements based on the results of the evaluation of Boolean expressions.
- Forward request - Forwards the request to the backend service.
- Limit concurrency
- Log to Event Hub - Sends messages in the specified format to an Event Hub defined by a Logger entity.
- Mock response
- Retry - ตรวจสอบงื่อนไข เพื่อทำต่อแทน Client
- Return response
- More Detail : Create advanced policies - Learn | Microsoft Docs
- Secure APIs
- using subscriptions - มี 2 key primary / secondary เอาไว้สลับกันใช้ เพื่อลด downtime โดย header ต้องใส่ Ocp-Apim-Subscription-Key เข้าไป
curl --header "Ocp-Apim-Subscription-Key: <key string>" https://<apim gateway>.azure-api.net/api/path
- using certificates - ดูจาก certificates ที่มี thumbprint ตรงกัน ต้องแก้ XML ของ Policy เพิ่มด้วย
More Detail : Secure APIs by using certificates - Learn | Microsoft Docs
Knowledge check : Explore API Management (Knowledge check)
Reference
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.



