2018-08-10|2018-08-10 Eureka + MySQL to provide service
Step 1 Build the base Eureka-client project
- Build a project that acts as a Eureka-client
4.0.0
com.example
eureka-provider
0.0.1-SNAPSHOT org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
1.8
【2018-08-10|2018-08-10 Eureka + MySQL to provide service】org.springframework.boot
spring-boot-maven-plugin
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
Finchley.RELEASE
pom
import
- Create the main-entry for the app
Content ofsrc\main\java\app\EurekaClientApp.java
package app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class EurekaClientApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApp.class, args);
}
}
- Prepare
application.yml
to configure the client
Content ofapplication.yml
spring:
application:
name: eureka-clientserver:
port: 8766eureka:
client:
register-with-eureka: true
serviceUrl:
defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
Step 2 Add dependency to access MySQL through JPA
- Add the dependency in the
pom.xml
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
- Update the
application.yml
file, to define the parameters to MySQL
Content ofapplication.yml
spring:
application:
name: eureka-client
jpa:
database: MYSQL
show-sql: true
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://127.0.0.1:3306/xiaoyouhui
username: root
password: password123
driver-class-name: com.mysql.jdbc.Driverserver:
port: 8766eureka:
client:
register-with-eureka: true
serviceUrl:
defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
- Create the model-class
package app.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "member")
public class Member{
//fields
//getters & setters
}
- Create the repository
package app.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import domain.Member;
@Repository
public interface MemberRepository extends JpaRepository{}
Step 3 Add a controller to handle request The controller-class should under the package of the main-entry. Since Spring-boot will scan from the root-package.
Content ofsrc\main\java\app\controller\MainController.java
package app.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import domain.Member;
import repository.MemberRepository;
@RestController
public class MainController {
@Autowired
private MemberRepository memberRepository;
@RequestMapping("/greeting")
public Optional greeting(@RequestParam(value="https://www.it610.com/article/id", defaultValue="https://www.it610.com/article/na") String id) {
return memberRepository.findById(id);
}@RequestMapping("/greetings")
public List greetings() {
List list = memberRepository.findAll();
System.out.println(list.size());
return list;
}@GetMapping(path="/user/{id}")
public String findById(@PathVariable Long id) {
return "hello";
}@RequestMapping("/test")
public @ResponseBody String testm() {
return "test";
}
}
Step 4 Configure to open some Actuator end-point
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
shutdown:
enabled: false
References
- Spring document to access MySQL
- Spring document to build a restful service
- Blog post to configure Actuator
推荐阅读
- py连接mysql
- 2019-01-18Mysql中主机名的问题
- MySql数据库备份与恢复
- mysql|InnoDB数据页结构
- mysql中视图事务索引与权限管理
- MYSQL主从同步的实现
- MySQL数据库的基本操作
- javaweb|基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
- Python3|Python3 MySQL 数据库连接
- MySQL|MySQL 存储过程语法及实例