1. Components to be used in the project
2. Version correspondence:
3. Import the dependencies of SpringCloudAlibaba in gulimail common:
<!--springcloudalibaba--> <!--<dependencyManagement>Indicates dependency management, which will be implemented in the future<dependencies>introduce alibaba There is no need to write the version number for related dependencies--> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
4.nacos as the registry:
Introduce nacos service registration and discovery dependency in common:
<!--nacos Service registration discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
Download the nacos server and start:
Configure the registry address in the yml file:
#Configure the nacos server registry address cloud: nacos: discovery: server-addr: 127.0.0.1:8848
Add @ EnableDiscoveryClient annotation to the startup class: service registration discovery
@EnableDiscoveryClient
Name the service in the yml file:
#Define your own name for the service so that you can see the registered name in the registry application: name: gulimail-coupon
5. After registering the two services, test the remote call, which uses Feign component
(1) If the member service wants to call the coupon service, it needs to add an openfeign dependency to the member service, so that it has the ability to call other services.
<!--Remote call feign Component dependency, with the ability to call other services--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2) In the controller of the coupon service gulimail coupon, write all the coupon functions of a member:
@RequestMapping("/member/list") public R membercoupons(){ CouponEntity couponEntity = new CouponEntity(); couponEntity.setCouponName("10 minus 100"); return R.ok().put("coupons",Arrays.asList(couponEntity)); }
(3) The member service calls the method of querying all coupons of a member in the coupon service: in the member service:
Write an interface to tell spring cloud the remote service that this interface needs to call
Create feign package and CouponFeignService interface: (write @ feignclient ("gulimail coupon") annotation in the interface, write the service name to be called, which function to call, which method to introduce the coupon service, and write the path of the method in full)
package com.atguigu.gulimail.member.feign; import com.atguigu.common.utils.R; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("gulimail-coupon") public interface CouponFeignService { @RequestMapping("coupon/coupon/member/list") public R membercoupons(); }
(4) Open the remote call function and add comments in the startup class of member service
@EnableFeignClients(basePackages = "com.atguigu.gulimail.member.feign") and tell feign the path of the package. When the service is started, it will automatically scan the remote call interface under the feign package
package com.atguigu.gulimail.member; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /* * To call another service remotely: * 1.openfeign needs to be introduced * 2.Write an interface to tell spring cloud that this interface needs to call remote services * (1)Each method that declares an interface is a request that invokes which remote service * 3.Enable remote call function @ EnableFeignClients * * */ @EnableFeignClients(basePackages = "com.atguigu.gulimail.member.feign") @EnableDiscoveryClient @SpringBootApplication public class GulimailMemberApplication { public static void main(String[] args) { SpringApplication.run(GulimailMemberApplication.class, args); } }
(5) invoke coupon service in member controller:
@RestController @RequestMapping("member/member") public class MemberController { @Autowired private MemberService memberService; @Autowired private CouponFeignService couponFeignService; /* * Call coupon service * */ @RequestMapping("/coupons") public R test(){ MemberEntity memberEntity = new MemberEntity(); memberEntity.setNickname("Zhang San"); R membercoupons = couponFeignService.membercoupons(); return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons")); }
(6) At first, the service failed to start because of the corresponding version of springboot and springcloud:
Remote access succeeded: access path: http://localhost:8000/member/member/coupons
6.nacos as the configuration center:
Advantages of using the configuration center in the project: if the configuration center is not used, if the project needs to modify the configuration file, it needs to be repackaged and published, which is very troublesome. After using the configuration center, you only need to modify it in the configuration center, and the project does not need to be repackaged.
(1) Introduce nacos into comon as the dependency of the configuration center:
<!--nacos As a configuration center dependency--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
(2) Create the bootstrap.properties file in the coupon service: (this is the name specified by springboot, which will take precedence over application.properties/yml loading and reading)
a. Method to get local profile value: @ Value("${user.name}")
application.properties:
c.user.name= dlf
CouponController:
@RestController @RequestMapping("coupon/coupon") public class CouponController { @Autowired private CouponService couponService; @Value("${c.user.name}") private String name; @RequestMapping("/test") public R test(){ return R.ok().put("name",name); }
Successfully obtained:
b. Modify the configuration file and put it in the configuration center. Now create gulimail-coupon.properties in nacos. Because it was configured in bootstrap before, the service will automatically find the corresponding gulimail-coupon.properties in nacos
If you change dlf to zhangsan in the configuration center and want to dynamically read the changed value in the code, you need to add a comment in CouponController:
@RefreshScope
7. Namespace of Nacos configuration center:
a. Development, testing and online production environments (after release) may have different configurations in different environments. Different namespaces can be created to store the configurations of different environments for isolation:
(1) New namespace:
(2) Configure in the development environment namespace:
(3) Specify the configuration using the dev namespace in bootstrap.properties:
#Name of the current service spring.application.name=gulimail-coupon #Address of configuration center spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #Specifies which namespace to use spring.cloud.nacos.config.namespace=89700824-406b-49db-b2e1-97b6510a7a3f
(4) Test successful:
b. If each service has too many configuration files and many services, each micro service can be isolated from each other, and each micro service creates its own namespace:
c. Configuration grouping: by default, all configuration sets belong to: DEFAULT_DROUP
We can customize the group and specify which group to use in the bootstrap:
#Name of the current service spring.application.name=gulimail-coupon #Address of configuration center spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #Specifies which namespace to use spring.cloud.nacos.config.namespace=c3849876-f08a-48fc-8d08-e7155ec4dc25 #Specify which group profile to use (default group if not configured) spring.cloud.nacos.config.group=1111
d. In the grain project, each microservice creates its own namespace and uses configuration grouping to distinguish the environment: dev,test, prop
#Name of the current service spring.application.name=gulimail-coupon #Address of configuration center spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #Specifies which namespace to use spring.cloud.nacos.config.namespace=c3849876-f08a-48fc-8d08-e7155ec4dc25 #Specify which group profile to use (default group if not configured) spring.cloud.nacos.config.group=dev
8. Load multiple configuration sets for one service: (migrate all configurations to nacos)
With the continuous growth of the business, there may be many configurations in a service. You can't write all the configurations in one file. It's so many, messy and difficult to maintain. It needs to be split into different configuration files: the configuration related to the data source is written in one configuration, the · configuration of the mybatisplus framework is written in one configuration, and the configuration related to micro services is written in one configuration.
#Name of the current service spring.application.name=gulimail-coupon #Address of configuration center spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #Specifies which namespace to use spring.cloud.nacos.config.namespace=c3849876-f08a-48fc-8d08-e7155ec4dc25 #Specify which group profile to use (default group if not configured) spring.cloud.nacos.config.group=dev spring.cloud.nacos.config.ext-config[0].data-id=datasouce.yml spring.cloud.nacos.config.ext-config[0].group=dev spring.cloud.nacos.config.ext-config[0].refresh=true spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml spring.cloud.nacos.config.ext-config[1].group=dev spring.cloud.nacos.config.ext-config[1].refresh=true spring.cloud.nacos.config.ext-config[2].data-id=other.yml spring.cloud.nacos.config.ext-config[2].group=dev spring.cloud.nacos.config.ext-config[2].refresh=true