I am also a beginner, if there are suggestions please leave a message Oh!
The next blog explains my recent feelings and understanding of learning spring cloud.
This blog is mainly through a simple demo to deepen the understanding of spring cloud.
1. A parent project is first built to collect the common jars that are used in the subproject
For a project with a spring cloud architecture, it's definitely necessary to bring in spring cloud and spring boot.
For projects, it is common to introduce test jar and log jar
1<parent>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-parent</artifactId>4<version>1.5.2.RELEASE</version>5<relativePath/>6<!--lookup parent from repository--7</parent>8<!--using spring cloud must be introduced--9<dependencyManagement>Ten<dependencies> One<dependency> A<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-dependencies</artifactId> -<version>Dalston.SR2</version> the<type>pom</type> -<scope>Import</scope> -</dependency> -</dependencies> +</dependencyManagement> -<dependencies> +<dependency> A<groupId>org.springframework.boot</groupId> at<artifactId>spring-boot-starter</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> in<!--Logback + slf4j-- -<dependency> to<groupId>ch.qos.logback</groupId> +<artifactId>logback-classic</artifactId> -</dependency> the<dependency> *<groupId>org.slf4j</groupId> $<artifactId>jcl-over-slf4j</artifactId>Panax Notoginseng</dependency> -<!--test modules, including JUnit, Hamcrest, Mockito, and the<dependency> +<groupId>org.springframework.boot</groupId> A<artifactId>spring-boot-starter-test</artifactId> the<scope>test</scope> +</dependency> -<!--https://Mvnrepository.com/artifact/com.alibaba/fastjson-<dependency> $<groupId>com.alibaba</groupId> $<artifactId>fastjson</artifactId> -<version>1.2.37</version> -</dependency> the<dependency> -<groupId>org.springframework.boot</groupId>Wuyi<artifactId> Spring-boot-configuration-processor </artifactId> the</dependency> -</dependencies>
2. Next, you need a public profile (obtained from GIT/SVN) that the project will use to save the projects, and Rest mode for other services to get configuration information.
Two different ways:
(1) Git
Application.properties
server.port=8888
Application-gitsimple.properties
# Specify the GIT project path where the configuration file is located spring.cloud.config.server.git.uri=https://github.com/hryou0922/spring_cloud.git# Indicates that the configuration file under this folder will be searched Spring.cloud.config.server.git.searchpaths=cloud-config-git/simple
Serverapplication:
@SpringBootApplication @enableconfigserver//Activate the app for Profile server: Read remote profile, convert to rest Interface Service public class public static void Main ( String[] args) {args = new string[1];args[0] = "--spring.profiles.active=gitsimple"; Springapplication.run (Cloudgitconfigserverapplication.class, args); The git configuration file is placed in this project: Cloud-config-git has two files in simple, the name and content are as follows: Cloud-config-dev.properties:simple.config.name= zoneregistercenter.eureka.defaultzone=http://localhost:8761/eureka/of git-devsimple.config.age=112# Registration service cloud-config-test.properties:simple.config.name=git-testsimple.config.age=1# zoneregistercenter.eureka.defaultzone=http://localhost:8761/eureka/of registered Services
- Simple.config.name and Simple.config.age as test data for subsequent service and customer read configurations.
- Registercenter.eureka.defaultzone: Service registered to the zone of service registration
(2) native
Project name: cloud-config-center config/simple creation profile in Resoucres directory cloud-config-dev.propertiessimple.config.name=native_devsimple.config.age=113# zoneregistercenter.eureka.defaultzone=http://localhost:8761/eureka/ of registered Services cloud-config-test.propertiessimple.config.name=native_testsimple.config.age=1# zoneregistercenter.eureka.defaultzone=http://localhost:8761/eureka/ of registered Services application-nativesimple.propertiesserver.port=8888# Native: Start reading the configuration file locally, you must specify the value of active, Before you can use this farm configuration mode spring.profiles.active=native# Custom profile path Spring.cloud.config.server.native.searchlocations=classpath :/config/simple/ cloudnativeconfigserverapplication@[email protected]//Activate the app for Profile server: Read remote configuration file, Convert to rest Interface Service public class Cloudnativeconfigserverapplication { public static void main (string[] args) {args = new Stri ng[1];//use native can not use native mode using spring.profiles.active//args[0] = "--spring.profiles.active=nativesimple"; args [0] = "--spring.config.name=application-nativesimple"; Springapplication.rUn (cloudnativeconfigserverapplication.class, args);}}
(3) After the simple, began to create the registration center of the project Eureka, this I will not repeat, a lot of online.
(4) Server and client
The simple truth is that the server registers the service with the Eureka, and the client calls from the Eureka.
But you need to pay attention to two Fang:
1.feign Client Pass Value
- Simple: no parameter request
- Simplewithoneparam: Request with a single parameter
- Simplewithqry: With multiple parameter requests
- @Configuration
- @ConfigurationProperties (prefix = "Simple.config", Ignoreunknownfields = False)
- public class Simpleconfig {private string Name;private Integer age, public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;} @Overridepublic String toString () {return ' name= ' +name+ ' | age= ' + Age;}}
- Controller in
@Autowiredprivate Service service; /*** Service * @return * * @RequestMapping (value = "/simple") public Simpledto Simple () {return service.simple ();} Service: @Servicepublic class SimpleService {@Autowiredprivate simpleconfig simpleconfig;//Public simpledto simple () { Simpledto simpledto = new Simpledto (); Simpledto.setage (Simpleconfig.getage ()); Simpledto.setname ( Simpleconfig.getname ()); Simpledto.setrandomnum (new Random (). nextint); return simpledto;} 2. Fuse Hytrix processing, simple, in fact, is to rewrite the client class, the inside of the method to rewrite, there is a retreat
The whole process is almost like this, I also look at the online materials learned, very useful.
Start the configuration center, start the registry, then start the server, and finally start the client.
Well, I've deepened this part of the story, and you, if you think it's helpful, give it to me.
Spring Cloud Demo Explanation