Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

My opinion on Micro Service Spring Cloud Alibaba

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Author: Piotr Mi Piotr Mi kowski, author of "Mastering Spring Cloud"

Original text link:

Https://dzone.com/articles/microservices-with-spring-cloud-alibaba

If you are looking for an alternative to Spring Cloud Netflix, it is recommended to take a look at this Spring Cloud Alibaba-related article.

Some time ago, Spring Cloud announced on its official blog that Alibaba Open Source Spring Cloud Alibaba has released the first preview version 0.2.0, which is compatible with Spring Boot 2.0. the project supports the construction of micro service system based on Alibaba's open source components and Ali Yunyun products.

This project looks very interesting and has become one of the most popular projects in the SpringCloud incubator repository.

Spring Cloud also supports another popular Alibaba open source component, Sentinel, which is responsible for flow control, concurrency, outage, and load protection.

Our demonstration example consists of three microservices and API gateways, which is very similar to the architecture described in my previous article, "Quick Guide to Building Micro Services based on SpringBoot 2.0, Eureka, and Spring Cloud."

The only difference is the tools used for configuration management and service discovery. The micro service invokes the interface exposed by the service, while department-service invokes the interface exposed by employee-service, using the OpenFeign client to implement the communication between services. The complexity of the entire system is hidden behind the API gateway implemented using NetflixZuul.

"can SpringCloud Alibaba replace SpringCloud Netflix?"

The answer is yes, but not all. Spring Cloud Alibaba is still integrated with Ribbon, and Ribbon is load balancing based on service discovery. In this case, Netflix Eureka is likely to be replaced by Nacos.

Nacos (DynamicNaming and Configuration Service) is an easy-to-use platform for dynamic service discovery, configuration management, and service management that makes it easier to build cloud native applications. According to this definition, you can use Nacos for:

Service Discovery-you can register your microservices and discover other microservices through the DNS or HTTP interface. It also provides real-time health checks for registration services.

Distributed configuration-the dynamic configuration service provided by Nacos allows you to manage the configuration of all services in a centralized and dynamic manner in all environments. In fact, you can also use it to replace Spring Cloud Config Server.

Dynamic DNS-- supports weighted routing, which makes it easier to achieve middle-tier load balancing, flexible routing policies, flow control and simple DNS parsing services.

Spring Cloud also supports another popular Alibaba open source component, Sentinel, which is responsible for flow control, concurrency, outage, and load protection.

Our demonstration example consists of three microservices and API gateways, which is very similar to the architecture described in my previous article, "Quick Guide to Building Micro Services based on SpringBoot 2.0, Eureka, and Spring Cloud." The only difference is the tools used for configuration management and service discovery.

The micro service invokes the interface exposed by the service, while department-service invokes the interface exposed by employee-service, using the OpenFeign client to implement the communication between services. The complexity of the entire system is hidden behind the API gateway implemented using NetflixZuul.

1. Run the Nacos server

You can run Nacos on Windows and Linux systems. First, you should download the latest stable version available on GitHub. After unzipping, you must run it in stand-alone mode by executing the following command:

Cmd nacos/bin/startup.cmd-m standalone

By default, Nacos starts at port 8848. It provides HTTP API under / nacos/v1 and the administrative web console under the address http://localhost:8848/nacos. If you look at the log, you will find that it is just an application written in SpringFramework.

two。 Dependency relationship

As I mentioned earlier, SpringCloud Alibaba is still in the incubation stage, so it is not included in SpringCloud Release Train. This is why we need to include a special BOM for Alibaba in the dependency management section of pom.xml. We will also use the latest stable version of Spring Cloud, which is now Finchley.SR2.

Org.springframework.cloudspring-cloud-dependenciesFinchley.SR2pomimportorg.springframework.cloudspring-cloud-alibaba-dependencies0.2.0.RELEASEpomimport

Spring Cloud Alibaba provides three initiators for currently supported components. These are spring-cloud-starter-alibaba-nacos-discovery for service discovery using Nacos, spring-cloud-starter-alibaba-nacos-config for distributed configuration, and spring-cloud-starter-alibaba-sentinel for flow-limiting degradation.

Org.springframework.cloudspring-cloud-starter-alibaba-nacos- discoveryorg.springframework.cloudspring-cloud-starter-alibaba-nacos- configorg.springframework.cloudspring-cloud-starter-alibaba-sentinel

3. Use Nacos to enable distributed configuration

To enable configuration management for Nacos, we only need to introduce a starter, spring-cloud-starter-alibaba-nacos-config. It does not provide the autoconfiguration address of the Nacos server, so we need to set it explicitly for the application in the bootstrap.yml file.

Spring:application:name: employee-servicecloud:nacos: config: server-addr: localhost:8848

Our application attempts to connect to Nacos and gets the configuration provided in the file with the same name as the value of the property spring.application.name. Currently, Spring Cloud Alibaba only supports .properties files, so we need to create the configuration in the file employee-service.properties.

Nacos provides an elegant way to create and manage configuration properties. We can use the network management console to do this. The field Data ID visible in the image below is actually the name of the configuration file. The list of configuration properties should be placed in the Configuration Content field.

The good news is that it refreshes the application configuration dynamically after modifying the Nacos. In your application, the only thing you need to do is comment on the bean that should be refreshed with @ RefreshScope or @ ConfigurationProperties.

Now, let's consider the following situation. We will modify the configuration slightly to add some properties with test data, as shown below.

This is the implementation of our repository bean. It injects all configuration attributes with the prefix repository.employees into the employees list.

@ Repository@ConfigurationProperties (prefix = "repository") public class EmployeeRepository {private List

< Employee >

Employees = new ArrayList

< >

(); public List

< Employee >

GetEmployees () {return employees;} public void setEmployees (List

< Employee >

Employees) {this.employees = employees;} public Employee add (Employee employee) {employee.setId ((long) (employees.size () + 1)); employees.add (employee); return employee;} public Employee findById (Long id) {Optional

< Employee >

Employee = employees.stream () .filter (a-> a.getId () .equals (id)) .findFirst (); if (employee.isPresent ()) return employee.get (); elsereturn null;} public List

< Employee >

FindAll () {return employees;} public List

< Employee >

FindByDepartment (Long departmentId) {return employees.stream () .filter (a-> a.getDepartmentId () .equals (departmentId)) .public List (Collectors.toList ());} public List

< Employee >

FindByOrganization (Long organizationId) {return employees.stream () .filter (a-> a.getOrganizationId () .equals (organizationId)) .notify (Collectors.toList ());}}

You can now change some property values, as shown in the following illustration. Then, if you call the employee-service available on port 8090 (http://localhost:8090), you should see a complete list of employees with modified values.

For our two other micro services, departmental services and organizational services, the same configuration properties should be created. Assuming you have finished, you should have the following configuration entry on Nacos.

4. Enable service discovery using Nacos

To use Nacos for service discovery, you first need to include starterspring-cloud-starter-alibaba-nacos-discovery. The same is true for the configuration server; you also need to set the address of the Nacos server in the bootstrap.yml file.

Spring: application: name: employee-service cloud: nacos: discovery: server-addr: localhost:8848

The final step is to enable the discovery client for the application by using the @ EnableDiscoveryClient annotation main class.

@ SpringBootApplication@EnableDiscoveryClient@EnableSwagger2public class EmployeeApplication {public static void main (String [] args) {SpringApplication.run (EmployeeApplication.class, args);}}

If you provide the same implementation for all microservices and run them, you will see the following list of registered applications in the Nacos Web console.

5. Communication between services

Communication between microservices is implemented using standard Spring Cloud components: RestTemplate or OpenFeign clients.

By default, load balancing is implemented by the Ribbon client. Compared with SpringCloud Netflix, the only difference before the service registry is SpringCloud Netflix. The following is the implementation of the integration of the FeignClient client responsible for communicating with the endpoint GET/department/ {departmentId} exposed by employee service in the departmental service.

@ FeignClient (name = "employee-service") public interface EmployeeClient {@ GetMapping ("/ department/ {departmentId}") List

< Employee >

FindByDepartment (@ PathVariable ("departmentId") Long departmentId);}

Don't forget to enable the Feign client for the Spring Boot application.

@ SpringBootApplication@EnableDiscoveryClient@EnableFeignClients@EnableSwagger2public class DepartmentApplication {public static void main (String [] args) {SpringApplication.run (DepartmentApplication.class, args);}}

We should also run multiple employee-service instances to test load balancing on the client side. Previously, we could enable dynamic generation of port numbers by setting the property server.port to 0 in the configuration stored on Nacos.

Now we can run many instances of a single service with the same configuration settings without having to worry about port number conflicts for a single microservice. Let's expand the number of employee-service instances.

If you want to test inter-service communication, you can call the following methods that use the OpenFeign client to invoke the endpoints exposed by other microservices: GET / organization/ {organizationId} / with-employees from department-service, and GET / {id} / with-departments, GET / {id} / with-departments-and-employees, GET / {id} / with-employees from organization-service.

6. Run API Gateway

This is now the last component in the running architecture, the API gateway. It is built on top of Spring Cloud Netflix Zuul and also uses Nacos as the discovery and configuration server.

Org.springframework.cloudspring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloudspring-cloud-starter-alibaba-nacos-configorg.springframework.cloudspring-cloud-starter-netflix-zuul

After including the required dependencies, we need to enable the Zuul proxy and the discovery client for the application.

@ SpringBootApplication@EnableDiscoveryClient@EnableZuulProxy@EnableSwagger2public class ProxyApplication {public static void main (String [] args) {SpringApplication.run (ProxyApplication.class, args);}}

The following is the configuration of the Zuul routes defined for our three example microservices:

Zuul: routes: department: path: / department/** serviceId: department-serviceemployee: path: / employee/** serviceId: employee-serviceorganization: path: / organization/** serviceId: organization-service

After running the gateway, it exposes the Swagger2 specification for the API exposed by all defined microservices. Assuming that you have run it on port 8080, you can access it under the address http://localhost:8080/swagger-ui.html. For this reason, you can call the method from a separate location.

7. Conclusion

The source code for the sample application is available on GitHub through sample-spring-microservices-new in the Alibaba branch.

The main purpose of this article is to show how to replace some popular SpringCloud components with AlibabaNacos for service discovery and configuration management.

The SpringCloud Alibaba project is in the early stages of development, so we may expect some new and interesting features in the near future. You can also find some other examples on the Spring Cloud Alibaba GitHub site here.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report